package com.snailxr.base.support.handler;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.snailxr.base.support.util.Util;
import com.snailxr.base.task.domain.jx.BSZN;
import com.trs.web2frame.WCMServiceCaller;
import com.trs.web2frame.dispatch.Dispatch;
import com.trs.web2frame.util.JsonHelper;

/**
 * 办事指南数据处理类,单例模式
 *  @author luoshijian
 *
 */
@Component("bsznHandler")
@Scope("singleton")
public class BSZNHandler {
	public final Logger log = Logger.getLogger(this.getClass());
	
	public BSZN handle(JSONObject item_json) {
		BSZN bszn = new BSZN();

		String sxmc = "";
		String slfw = "";
		String bljg = "";
		String jdtsdh = "";
		String sltj = "";
		String blcx = "";
		String lct = "";
		String blsx = "";
		String sfyj = "";
		String sfbz = "";
		String bgxz = "";
		String wsbl = "";
		String wscx = "";
		String zxdh = "";
		String blcs = "";
		String itemId = "";
		String CODE = "";
		String ORG_CODE = "";
		String blckhdz = "";
		String flyj = "";
		String sxcl = "";
		String kbl = "";
		String kzxblsj = "";
		String ssfl = "";

		String IS_CHARGE="";
		bszn.setObjectId(0);
		bszn.setChannelId(1196);
		
		sxmc = item_json.getString("NAME");
		
		blsx="法定期限："+item_json.getString("LAW_TIME")+"工作日   承诺期限:"+item_json.getString("AGREE_TIME")+"工作日";
		if("2".equals(item_json.getString("ASSORT"))){
			blsx="即办件";
		}
		ORG_CODE = item_json.getString("ORG_CODE");
		CODE = item_json.getString("CODE");
		itemId = item_json.getString("ID");
		ssfl = item_json.getString("TITLE_NAME");
		blcs = item_json.getString("APPLY_TIMES");
		IS_CHARGE = item_json.getString("IS_CHARGE");
		if("1".equals(IS_CHARGE)){
			try {
				JSONArray sfyj_array = JSONArray.parseArray(item_json.getString("farelList"));
				for (int j = 0; j < sfyj_array.size(); j++) {
					JSONObject sfyj_json=sfyj_array.getJSONObject(j);
					sfyj+="<span class='sfyj_con'><strong>收费名称：</strong>"+sfyj_json.getString("NAME")+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>收费依据：</strong>"+sfyj_json.getString("BASIS")+"</span><br/>";
					sfbz+="<span class='sfbz_con'><strong>收费名称：</strong>"+sfyj_json.getString("NAME")+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>收费标准：</strong>"+sfyj_json.getString("STANDARD")+"</span><br/>";
				}
			}catch (Exception e){
			}
		}else{
			sfyj="不收费";
			sfbz="不收费";
		}
		
			JSONArray flyj_array=JSONArray.parseArray(item_json.getString("doomList"));
			if(flyj_array.size()==1){
				JSONObject flyj_json=flyj_array.getJSONObject(0);
				flyj+="<span class='sfbz_con'><strong>法律名称：</strong>"+flyj_json.getString("LAW_NAME")+"；<br/><strong>条款名称：</strong>"+flyj_json.getString("NAME")+"；<br/><strong>条款内容：</strong>"+flyj_json.getString("CONTENT")+"</span><br/>";
			}else{
				for (int j = 0; j < flyj_array.size(); j++) {
					JSONObject flyj_json=flyj_array.getJSONObject(j);
					flyj+="<span class='flyj_n'>"+(j+1)+"</span><span class='flyj_dot'>.</span><span class='sfbz_con'><strong>法律名称：</strong>"+flyj_json.getString("LAW_NAME")+"；<br/>&nbsp;&nbsp;<strong>条款名称：</strong>"+flyj_json.getString("NAME")+"；<br/>&nbsp;&nbsp;<strong>条款内容：</strong>"+flyj_json.getString("CONTENT")+"</span><br/>";
				}
			}
			
		bljg=item_json.getString("ORG_NAME");
		
		slfw="个人";
		if(item_json.getString("SERVICE_OBJECT").indexOf("0,1")>-1){
			slfw="个人及法人";
		}else if(item_json.getString("SERVICE_OBJECT").indexOf("0")>-1){
			slfw="个人";
		}else if(item_json.getString("SERVICE_OBJECT").indexOf("1")>-1){
			slfw="法人";
		}
		
		JSONArray blcx_array=JSONArray.parseArray(item_json.getString("processList"));
		for (int j = 0; j < blcx_array.size(); j++) {
			JSONObject blcx_json=blcx_array.getJSONObject(j);
			blcx+="<span class='blcx_n'>"+(j+1)+"</span><span class='blcx_dot'>.</span><span class='blcx_con'>"+blcx_json.getString("CONTENT")+"</span><br/>";
			//out.print("<br/>"+j+"."+blcx+"<br/>");
		}
			
			JSONArray ckdh_array=JSONArray.parseArray(item_json.getString("venuesList"));
			for (int j = 0; j < ckdh_array.size(); j++) {
				JSONObject ckdh_json=ckdh_array.getJSONObject(j);
				zxdh=ckdh_json.getString("PHONE");
				jdtsdh=ckdh_json.getString("COMPLAINT_PHONE");
				blckhdz=ckdh_json.getString("NAME")+"&nbsp;&nbsp;"+ckdh_json.getString("ADDRESS");
				kzxblsj=ckdh_json.getString("OFFICE_HOUR");
			}
			
		JSONArray blcs_array=JSONArray.parseArray(item_json.getString("handlingList"));
		for (int j = 0; j < blcs_array.size(); j++) {
			JSONObject blcs_json=blcs_array.getJSONObject(j);
			if(blcs.equals("")){
				blcs=blcs_json.getString("REMARK");
			}
			wsbl=blcs_json.getString("BL"); //网上办理
			wscx=blcs_json.getString("CX");  //网上查寻
		}
			
		JSONArray sltj_array=JSONArray.parseArray(item_json.getString("conditionList"));
		if(sltj_array.size()==1){
			JSONObject sltj_json=sltj_array.getJSONObject(0);
			sltj+="<span class='blcx_con'>"+sltj_json.getString("NAME")+"</span><br/>";
		}else{
			for (int j = 0; j < sltj_array.size(); j++) {
				JSONObject sltj_json=sltj_array.getJSONObject(j);
				sltj+="<span class='sltj_n'>"+(j+1)+"</span><span class='blcx_dot'>.</span><span class='blcx_con'>"+sltj_json.getString("NAME")+"</span><br/>";
			}
		}
		JSONArray bgxz_array = JSONArray.parseArray(item_json.getString("documentList"));
		if(bgxz_array != null){
			for (int j = 0; j < bgxz_array.size(); j++) {
				//表格下载信息
				JSONObject bgxz_json=bgxz_array.getJSONObject(j);
				String BGXZ_NAME=bgxz_json.getString("FILENAME");
				if(!"null".equals(BGXZ_NAME)){
					int len=BGXZ_NAME.length();
					int begin=BGXZ_NAME.lastIndexOf(".");
					String ext=BGXZ_NAME.substring(begin+1,len);
					String filename=BGXZ_NAME.substring(0,begin);
					//String fileResult=downloadFromUrl("http://10.1.10.50:8090/WebDiskServerDemo/doc?doc_id="+bgxz_json.getString("SAMPLE"), "/home/file/bmfw_file/",ext);
					String fileResult= "Successful";//TODO 写死做测试
					if((!"null".equals(bgxz_json.getString("SAMPLE")))&&"Successful".equals(fileResult)){
						bgxz+=(j+1)+"、<a class='bgxz_a' href='http://www.jxzwfww.gov.cn/bmsx_file/bmfw_file/"+bgxz_json.getString("SAMPLE")+"."+ext.replaceAll(";","")+"'>"+BGXZ_NAME+"</a>"+"<br/>";//表格下载
					}
				}	
			}
		}
		
		JSONArray sxcl_array=JSONArray.parseArray(item_json.getString("materialList"));
		sxcl="<table class='table'>"
			 +"<tr>"
				+"<td class='top td-tit' align='center' style='width:300px'>材料名称</td>"
				+"<td class='top td-tit' align='center' style='width:100px'>是否必须</td>"
				+"<td class='top td-tit' align='center' style='width:100px'>原件份数</td>"
				+"<td class='top td-tit' align='center' style='width:100px'>复印件份数</td>"
				+"<td class='top td-tit' align='center' style='width:200px'>示范材料</td>"
				+"<td class='top td-tit' align='center' style='width:200px'>备注</td>"
			  +"</tr>";
		for (int j = 0; j < sxcl_array.size(); j++) {
			String sfwb_url="";
			String sysm="";
			String sxcl_bz="";
			
		    
			JSONObject sfwb_json=sxcl_array.getJSONObject(j);
			if("Apply".equals(sfwb_json.getString("BUSINESS_TYPE"))){
				String FILE_NAME = sfwb_json.getString("SAMPLE_NAME");
				if(!"".equals(FILE_NAME) && FILE_NAME != null){
					int len = FILE_NAME.length();
					int begin = FILE_NAME.lastIndexOf(".");
					String ext = FILE_NAME.substring(begin+1,len);
					String filename = FILE_NAME.substring(0,begin);
					
					//String fileResult=downloadFromUrl("http://10.1.10.50:8090/WebDiskServerDemo/doc?doc_id="+sfwb_json.getString("SAMPLE"), "/home/file/bmfw_file/",ext);
					String fileResult= "Successful";//TODO 写死做测试
					if((!"null".equals(sfwb_json.getString("SAMPLE")))&&"Successful".equals(fileResult)){
						if("null".equals(sfwb_json.getString("SAMPLE_NAME"))||"".equals(sfwb_json.getString("SAMPLE_NAME"))||sfwb_json.getString("SAMPLE_NAME")==null){
						}else{
							sfwb_url="<a class='bgxz_a' href='http://www.jxzwfww.gov.cn/bmsx_file/bmfw_file/"+sfwb_json.getString("SAMPLE")+"."+ext.replaceAll(";","")+"'>"+sfwb_json.getString("SAMPLE_NAME")+"</a>";//所需材料中示范文本链接地址
						}
					}else{
					}
				}
				
				//所需材料信息
				JSONObject sxcl_json=sxcl_array.getJSONObject(j);
				String must=sxcl_json.getString("MUST");
				if(must.equals("0")){
					must="否";
				}else{
					must="是";
				}
				sysm=sxcl_json.getString("WHAT");
				if("".equals(sysm)||"null".equals(sysm)){
					sysm="";
				}
				sxcl_bz=sxcl_json.getString("REMARK");
				if("".equals(sxcl_bz)||"null".equals(sxcl_bz)){
					sxcl_bz="";
				}
				sxcl+="<tr>"
					+"<td>"+sxcl_json.getString("NAME")+"</td>"
					+"<td align='center'>"+must+"</td>"
					+"<td align='center'>"+sxcl_json.getString("ORIGIN")+"</td>"
					+"<td align='center'>"+sxcl_json.getString("COPY")+"</td>"
					+"<td>"+sfwb_url+"</td>"
					+"<td>"+sxcl_bz+"</td>"
					+"</tr>";
			}
		}
		sxcl+="</table>";
		
		JSONArray lct_array = JSONArray.parseArray(item_json.getString("outProcessList"));
		for (int j = 0; j < lct_array.size(); j++) {
			JSONObject lct_json = lct_array.getJSONObject(j);
			String FILE_NAME = lct_json.getString("FILE_NAME");
			int len = FILE_NAME.length();
			int begin = FILE_NAME.lastIndexOf(".");
			String ext = FILE_NAME.substring(begin+1,len);
			String filename = FILE_NAME.substring(begin);
			//String fileResult = downloadFromUrl("http://10.1.10.50:8090/WebDiskServerDemo/doc?doc_id="+lct_json.getString("URL"), "/home/file/bmfw_file/",ext);
			String fileResult= "Successful";//TODO 写死做测试
			if((!"null".equals(lct_json.getString("URL")))&&"Successful".equals(fileResult)){
				if(ext.indexOf("png")>-1||ext.indexOf("jpg")>-1||ext.indexOf("gif")>-1){
					lct+="<img class='lct' src='http://www.jxzwfww.gov.cn/bmsx_file/bmfw_file/"+lct_json.getString("URL")+"."+ext+"'/><br/>";
				}else{
					lct+="<a class='lct' href='http://www.jxzwfww.gov.cn/bmsx_file/bmfw_file/"+lct_json.getString("URL")+"."+ext+"'>办事流程图资料</a><br/>";
				}
			}
		}
		bszn.setSxmc(sxmc);
		bszn.setSlfw(slfw);
		bszn.setBljg(bljg);
		bszn.setJdtsdh(jdtsdh);
		bszn.setSltj(sltj);
		bszn.setBlcx(blcx);
		bszn.setLct(lct);
		bszn.setBlsx(blsx);
		bszn.setSfyj(sfyj);
		bszn.setSfbz(sfbz);
		bszn.setBgxz(bgxz);
		bszn.setWsbl(wsbl);
		bszn.setWscx(wscx);
		bszn.setZxdh(zxdh);
		bszn.setBlcs(blcs);
		bszn.setItemId(itemId);
		bszn.setCODE(CODE);
		bszn.setORG_CODE(ORG_CODE);
		bszn.setBlckhdz(blckhdz);
		bszn.setFlyj(flyj);
		bszn.setSxcl(sxcl);
		bszn.setKbl(kbl);
		bszn.setKzxblsj(kzxblsj);
		bszn.setSsfl(ssfl);
		return bszn;
	}
	
	
	/**
	 * 将行政审批数据保存至wcm
	 * @param xzsp
	 */
	public String saveMetaViewData(BSZN bszn){
		String sServiceId = "wcm6_MetaDataCenter";//服务名
		String sMethodName = "saveMetaViewData";//方法名
		Map<String,Object> oPostData = Util.ConvertObjToMap(bszn);//bean对象转map
		Dispatch oDispatch = WCMServiceCaller.Call(sServiceId, sMethodName,oPostData, true);//保存到办事指南
		Map<String,Object> ojson = oDispatch.getJson();
		System.out.println("#######################"+ojson.get("METAVIEWDATA"));
		String metaDataid = JsonHelper.getValueAsString(ojson, "METAVIEWDATA.METADATAID");//获取按主题分类的相关文档ID
		
		return metaDataid;
	}
}
